package com.woniuxy.order.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.order.dao.EntranceInfo;
import com.woniuxy.order.model.dto.EntranceInfoDto;
import com.woniuxy.order.model.form.CarPullOutForm;
import com.woniuxy.order.model.form.LoadAllCarForm;
import com.woniuxy.order.model.param.CarPullOutParam;
import com.woniuxy.order.model.param.LoadAllCarParam;
import com.woniuxy.order.service.EntranceInfoService;
import com.woniuxy.order.service.param.AddCarParam;
import com.woniuxy.order.web.controller.form.AddCarForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.parser.Token;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@RequestMapping("/entranceInfo")
@Api(tags = "车辆管理接口")
public class EntranceInfoController {

    @Resource
    private EntranceInfoService entranceInfoService;

    @ApiOperation("新增入场车辆信息")
    @PostMapping("/addCar")
    public Result addCar(@RequestBody AddCarForm acf, @RequestHeader String token) throws ParseException {
        System.out.println(acf);
        String id = JwtUtil.parseAdminToken(token).get("id").toString();
        AddCarParam param = BeanUtil.toBean(acf, AddCarParam.class);
        entranceInfoService.addCar(param,id);
        return Result.getResult().setMessage("新增车辆成功");
    }

    @ApiOperation("车辆出场")
    @PostMapping("/carPullOut")
    public Result carPullOut(@RequestBody CarPullOutForm cpf) throws ParseException {
        CarPullOutParam param = BeanUtil.toBean(cpf, CarPullOutParam.class);
        entranceInfoService.carPullOut(param);
        return Result.getResult().setMessage("车辆驶出成功");
    }


    @ApiOperation("加载所有车辆信息")
    @PostMapping("/loadAllCar")
    private Result<PageInfo> loadAllCar(@RequestBody LoadAllCarForm laf,@RequestHeader String token){
        String id = JwtUtil.parseAdminToken(token).get("id").toString();
        LoadAllCarParam param = BeanUtil.toBean(laf, LoadAllCarParam.class);
        PageInfo<EntranceInfoDto> dto =entranceInfoService.loadAllCar(param,id);
        return Result.getResult(PageInfo.class).setData(dto).setMessage("加载所有车辆信息成功");
    }

    @ApiOperation("根据id删除车辆信息")
    @GetMapping("/deleteCar")
    @ApiImplicitParam(name = "entranceInfoId",value = "车辆信息id")
    private Result deleteCar(Integer entranceInfoId){
        System.out.println(entranceInfoId);
        entranceInfoService.deleteCar(entranceInfoId);
        return Result.getResult().setMessage("删除车辆信息成功");
    }

}

